/*******************************************************************************
 * Copyright 2020 huanggefan.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

const IndexPage = new HtmlWebpackPlugin({
    template: path.resolve(__dirname, "src/pages/index/index.html"),
    filename: path.resolve(__dirname, "dist/index.html"),
    chunks: ['api', 'style', 'index']
});
const StudentPage = new HtmlWebpackPlugin({
    template: path.resolve(__dirname, "src/pages/student/index.html"),
    filename: path.resolve(__dirname, "dist/student.html"),
    chunks: ['api', 'style', 'student']
});
const StudentCoursePage = new HtmlWebpackPlugin({
    template: path.resolve(__dirname, "src/pages/student/student-course.html"),
    filename: path.resolve(__dirname, "dist/student-course.html"),
    chunks: ['api', 'style', 'student']
});
const StudentPracticePage = new HtmlWebpackPlugin({
    template: path.resolve(__dirname, "src/pages/student/student-practice.html"),
    filename: path.resolve(__dirname, "dist/student-practice.html"),
    chunks: ['api', 'style', 'student']
});
const StudentHomeworkPage = new HtmlWebpackPlugin({
    template: path.resolve(__dirname, "src/pages/student/student-homework.html"),
    filename: path.resolve(__dirname, "dist/student-homework.html"),
    chunks: ['api', 'style', 'student']
});
const StudentExamPage = new HtmlWebpackPlugin({
    template: path.resolve(__dirname, "src/pages/student/student-exam.html"),
    filename: path.resolve(__dirname, "dist/student-exam.html"),
    chunks: ['api', 'style', 'student']
});
const StudentDataPage = new HtmlWebpackPlugin({
    template: path.resolve(__dirname, "src/pages/student/student-data.html"),
    filename: path.resolve(__dirname, "dist/student-data.html"),
    chunks: ['api', 'style', 'student']
});
const StudentCourseChaptersPage = new HtmlWebpackPlugin({
    template: path.resolve(__dirname, "src/pages/student/student-course-chapters.html"),
    filename: path.resolve(__dirname, "dist/student-course-chapters.html"),
    chunks: ['api', 'style', 'student']
});
const StudentCourseContentPage = new HtmlWebpackPlugin({
    template: path.resolve(__dirname, "src/pages/student/student-course-content.html"),
    filename: path.resolve(__dirname, "dist/student-course-content.html"),
    chunks: ['api', 'style', 'student']
});
const RootPage = new HtmlWebpackPlugin({
    template: path.resolve(__dirname, "src/pages/root/index.html"),
    filename: path.resolve(__dirname, "dist/root.html"),
    chunks: ['api', 'style', 'root']
});
const ManagerPage = new HtmlWebpackPlugin({
    template: path.resolve(__dirname, "src/pages/manager/index.html"),
    filename: path.resolve(__dirname, "dist/manager.html"),
    chunks: ['api', 'style', 'manager']
});
const ManagerCollegePage = new HtmlWebpackPlugin({
    template: path.resolve(__dirname, "src/pages/manager/manager-college.html"),
    filename: path.resolve(__dirname, "dist/manager-college.html"),
    chunks: ['api', 'style', 'manager']
});
const ManagerMajorPage = new HtmlWebpackPlugin({
    template: path.resolve(__dirname, "src/pages/manager/manager-major.html"),
    filename: path.resolve(__dirname, "dist/manager-major.html"),
    chunks: ['api', 'style', 'manager']
});
const ManagerClassPage = new HtmlWebpackPlugin({
    template: path.resolve(__dirname, "src/pages/manager/manager-class.html"),
    filename: path.resolve(__dirname, "dist/manager-class.html"),
    chunks: ['api', 'style', 'manager']
});
const ManagerCoursePage = new HtmlWebpackPlugin({
    template: path.resolve(__dirname, "src/pages/manager/manager-course.html"),
    filename: path.resolve(__dirname, "dist/manager-course.html"),
    chunks: ['api', 'style', 'manager']
});
const ManagerQuestionPage = new HtmlWebpackPlugin({
    template: path.resolve(__dirname, "src/pages/manager/manager-question.html"),
    filename: path.resolve(__dirname, "dist/manager-question.html"),
    chunks: ['api', 'style', 'manager']
});
const ManagerUserPage = new HtmlWebpackPlugin({
    template: path.resolve(__dirname, "src/pages/manager/manager-user.html"),
    filename: path.resolve(__dirname, "dist/manager-user.html"),
    chunks: ['api', 'style', 'manager']
});
const SettingPage = new HtmlWebpackPlugin({
    template: path.resolve(__dirname, "src/pages/setting/index.html"),
    filename: path.resolve(__dirname, "dist/setting.html"),
    chunks: ['api', 'style', 'setting']
});

const devServerConf = {
    contentBase: path.resolve(__dirname, "dist"),
    inline: true,
    host: "::",
    port: 8000,
    allowedHosts: ['*'],
    useLocalIp: true,
    compress: false,
    index: "index.html",
    publicPath: "/",
    open: false,
    stats: "errors-only",
    hot: true
};

module.exports = {
    entry: {
        api: path.resolve(__dirname, "src/api/api.js"),
        style: path.resolve(__dirname, "src/style/style.js"),

        index: path.resolve(__dirname, "src/pages/index/index.js"),
        student: path.resolve(__dirname, "src/pages/student/student.js"),
        root: path.resolve(__dirname, "src/pages/root/root.js"),
        manager: path.resolve(__dirname, "src/pages/manager/manager.js"),
        setting: path.resolve(__dirname, "src/pages/setting/setting.js")
    },
    output: {
        filename: "js/[name].[hash].js",
        path: path.resolve(__dirname, "dist"),
        publicPath: "/"
    },
    devServer: devServerConf,
    stats: {
        children: false
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: "css/[name].[hash].css",
            chunkFilename: "css/[id].[hash].css"
        }),
        IndexPage,
        StudentPage,
        StudentCoursePage,
        StudentPracticePage,
        StudentHomeworkPage,
        StudentExamPage,
        StudentDataPage,
        StudentCourseChaptersPage,
        StudentCourseContentPage,
        RootPage,
        ManagerPage,
        ManagerCollegePage,
        ManagerMajorPage,
        ManagerClassPage,
        ManagerCoursePage,
        ManagerQuestionPage,
        ManagerUserPage,
        SettingPage
    ],
    module: {
        rules: [
            {
                test: /\.html$/,
                use: {
                    loader: 'html-loader',
                    options: {
                        attrs: ['img:src', 'link:href', 'script:src']
                    }
                }
            },
            {
                test: /\.css$/,
                use: [MiniCssExtractPlugin.loader, "css-loader"]
            },
            {
                test: /\.(eot|woff|ttf|ico|png|svg|jpg)$/,
                use: {
                    loader: 'url-loader',
                    options: {
                        name: '[name].[hash].[ext]',
                        outputPath: "assets",
                        publicPath: "/assets",
                        limit: 2048,
                        esModule: false
                    }
                }
            }
        ]
    }
};
